package com.sunlands.zlcx.usercenter.service;

import com.sunlands.zlcx.usercenter.domain.UsersDO;
import com.sunlands.zlcx.usercenter.repository.UsersRepository;
import com.sunlands.zlcx.usercenter.util.BeanPropertiesUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author anjl
 * @date 1:33 PM 2019/4/15
 * description: 用户处理业务相关的userserice
 */
@Service
@Slf4j
public class UsersPoolServiceImpl implements BeanPropertiesUtil {

    /**
     * 查询近期学习时长排名靠前的用户
     */
    public synchronized List<UsersDO> learnTimeTopUsers(Integer page, Integer pageSize, Date startTime, Date endTime) {
        int start = (page - 1) * pageSize;
        List<Integer> longs = usersRepository.learnTimeTopUsers(start, pageSize, startTime, endTime);
        if(longs.isEmpty()){
            return Collections.emptyList();
        }
        List<Long> collect = longs.stream().map(l -> Long.parseLong(l.toString())).collect(Collectors.toList());
        return usersRepository.findByIdInOrderByNameAsc(collect);
    }

    @Autowired
    private UsersRepository usersRepository;

}
